package com.zhuchaooasystem.presenter.home.fragment.oa.sign;

import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;

import com.baidu.location.LocationClientOption;
import com.socks.library.KLog;
import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.HttpUtil;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.SignNowEntity;
import com.zhuchaooasystem.entity.UserInfo;
import com.zhuchaooasystem.utils.CalendarUtils;
import com.zhuchaooasystem.utils.EncryptUtils;
import com.zhuchaooasystem.utils.ToastUtil;
import com.zhuchaooasystem.widget.ConfirmDialog;

import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * @author yinsi
 */


public class SignNowPresenter extends BasePresenter<SignNowContract.View> implements SignNowContract.Presenter {

    public SignNowPresenter(Activity activity, SignNowContract.View view) {
        super(activity, view);
    }

    /**
     * 配置定位功能
     */
    public LocationClientOption getLocationClientOption() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        option.setScanSpan(3000);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.disableCache(true);
        option.SetIgnoreCacheException(true);
        return option;
    }

    @Override
    public void sign() {
        //----
        final int userId = UserInfo.getInstance().getMemberId();
        final String address = mView.getAddress();
        double latitude = mView.getLatitude();
        double longitude = mView.getLongitude();
        //----
        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("PersonnelPK", userId + "");
        map.put("latitude", latitude + "");
        map.put("longitude", longitude + "");
        map.put("Address", address);
        map.put("Device", EncryptUtils.getuniqueId(mActivity) + "*" + Build.MODEL);

        Subscription subscription = HttpFactory.getHttpApi().punchRecords(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        KLog.i("返回", o.toString());
                        SignNowEntity entity = (SignNowEntity) o;
                        if (null != entity) {
                            mView.signFinish(entity.getResult());
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }

    @Override
    public void showDialog() {
        final String address = mView.getAddress();
        double latitude = mView.getLatitude();
        double longitude = mView.getLongitude();
        if (TextUtils.isEmpty(address) || latitude == (double) 0 || longitude == (double) 0) {
            ToastUtil.showShort(mActivity, "数据错误，请重新定位");
        } else {
            //----
            ConfirmDialog confirmDialog = new ConfirmDialog(mActivity);
            confirmDialog.show();
            confirmDialog.setTextTitle("打卡确认");
            confirmDialog.setTextContent("打卡时间：" + CalendarUtils.getNow("yyyy-MM-dd HH:mm:ss") + "\n打卡位置：" + address);
            confirmDialog.setOnViewClickListener(new ConfirmDialog.OnViewClickListener() {
                @Override
                public void onClickOk(ConfirmDialog dialog) {
                    dialog.dismiss();
                    sign();
                }

                @Override
                public void onClickCancel(ConfirmDialog dialog) {
                    dialog.dismiss();
                }
            });
        }

    }

}
